/**
 * Copyright (c) 2012-2014 All Rights Reserved.
 */
package com.easy.framwork.utils;

import java.io.File;
import java.net.URISyntaxException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.easy.framwork.exceptions.IllegalArgException;

/**
 * 
 * @author wy
 * @version v 0.1 2014-1-5 下午11:33:13 wy Exp $
 */
public class ResourceUtils {

    private final static Logger log = LoggerFactory.getLogger(ResourceUtils.class);

    public final static String CLASS_PATH_PREFIX = "classpath:";
    
    /**
     * 获取文件
     * 
     * @param path
     * @return
     */
    public static File getFile(String path) {

        if (StringUtils.isBlank(path)) {
            throw new IllegalArgException("路径不能为空");
        } else if (path.startsWith(CLASS_PATH_PREFIX)) {
            String p = path.substring(CLASS_PATH_PREFIX.length());
            return new File(getClassPath() + (p.startsWith("/") ? p : "/" + p));
        }
        return new File(path);
    }

    /**
     * 获取当前文件路径
     * 
     * @return
     */
    public static String getClassPath() {
        String classPath = "/";

        try {
            classPath = Thread.currentThread().getContextClassLoader().getResource("").toURI()
                .getPath();
        }
        catch (URISyntaxException e) {
            log.warn("获取classpath路径错误", e);
        }
        return classPath;
    }

}
